package safari.controller.faq;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import safari.model.common.ListVO;
import safari.model.faq.FAQBoardVO;
import safari.model.faq.FAQServiceImpl;

public class FAQController extends MultiActionController {
	private FAQServiceImpl service;
	
	public FAQController(FAQServiceImpl service) {
		super();
		this.service = service;
	}
	/**
	 * 	게시판에 글 등록 method
	 * insert를 한 후 
	 * 등록한 글번호를 get한 뒤 
	 * redirect로 상세보기 메서드 호출
	 */
	public ModelAndView writeBoard(HttpServletRequest request, HttpServletResponse response, FAQBoardVO fvo) throws SQLException{
		service.writeBoard(fvo);
		System.out.println(fvo);
		int no = fvo.getNo();
		return new ModelAndView("redirect:/faq.do?command=getBoardByNo&no="+no);
	}
	/**
	 * 글 삭제 method
	 */
	public String deleteBoard(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int no = Integer.parseInt(request.getParameter("no"));
		service.deleteBoard(no);
		return "faq/main";
	}
	public ModelAndView moveToUpdateBoard(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int no = Integer.parseInt(request.getParameter("no"));
		return new ModelAndView("faq/update","fvo", service.getBoardByNo(no));
	}
	public ModelAndView updateBoard(HttpServletRequest request, HttpServletResponse response, FAQBoardVO fvo) throws SQLException{
		System.out.println("업데이트 전:"+fvo);
		service.updateBoard(fvo);
		System.out.println("업데이트 후:"+fvo);
		int no = fvo.getNo();
		return new ModelAndView("redirect:/faq.do?command=getBoardByNo&no="+no);
	}
	public ModelAndView updateBoardHits(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int no= Integer.parseInt(request.getParameter("no"));
		int hits = service.updateBoardHits(no);
		return new ModelAndView("JsonView", "hits", hits);
		
	}
	public ModelAndView getBoardByNo(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int no = Integer.parseInt(request.getParameter("no"));
		FAQBoardVO fvo = service.getBoardByNo(no);
		System.out.println(fvo);
		return new ModelAndView("faq/content", "fvo", fvo);
	}
	public String moveToFAQList(HttpServletRequest request, HttpServletResponse response){
		return "faq/main";
	}
	public String moveToFAQWrite(HttpServletRequest request, HttpServletResponse response){
		return "faq/write";
	}
	public ModelAndView getAllBoard(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		ListVO lvo= service.getAllBoard();
		System.out.println("전체"+lvo);
		return new ModelAndView("/faq/category/category1","lvo",lvo);
	}
	public ModelAndView findBoardByCategory(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String path="";
		String category=request.getParameter("category");
		ListVO lvo=service.findBoardByCategory(category);
		if(category.equals("가입/탈퇴")){
			path="/faq/category/category2";
		}
		else if(category.equals("개인정보")){
			path="/faq/category/category3";
		}
		else if(category.equals("물품 판매")){
			path="/faq/category/category4";
		}
		else if(category.equals("물품 구매")){
			path="/faq/category/category5";
		}
		
		else if(category.equals("포인트 결제")){
			path="/faq/category/category6";
		}
		else if(category.equals("게시판 이용")){
			path="/faq/category/category7";
		}
		else if(category.equals("기타")){
			path="/faq/category/category8";
		}
		System.out.println("카테고리"+lvo);
		return new ModelAndView(path,"lvo",lvo);
	}
	public ModelAndView findBoardByTitle(HttpServletRequest request, HttpServletResponse response){
		return null;
	}
	public ModelAndView findBoardByContent(HttpServletRequest request, HttpServletResponse response){
		return null;
	}
	public ModelAndView findBoardByTitleOrContent(HttpServletRequest request, HttpServletResponse response){
		return null;
	}
}
